package com.atjava.nebula.eims.comdoc.service;

import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Transactional;

import com.atjava.nebula.core.exception.baseExp.BusinessException;
import com.atjava.nebula.core.service.BaseService;
import com.atjava.nebula.core.service.impl.GenericServiceImpl;
import com.atjava.nebula.eims.comdoc.model.MoDocDef;
import com.atjava.nebula.eims.comdoc.model.MoDocValue;
import com.atjava.nebula.web.constant.ProjectConst;

public class CommonDocServiceImpl extends GenericServiceImpl<MoDocDef,Short> implements CommonDocService {
	
	private BaseService baseService;

	@Transactional
	public void importDocument(MoDocDef docDef, String[][] data) throws BusinessException {
		
		int hdRowIndex = 0;
		if(data.length-1 < hdRowIndex){
			throw new BusinessException("Error file layout, data row number:["+data.length+"], header index ["+hdRowIndex+"].");
		}
		
		Short docId = docDef.getPkid();
		
		if(docId == null){
			String[] header = data[hdRowIndex];
			StringBuffer headerString = new StringBuffer();
			for (int i = 0; i < header.length; i++) {
				if(i!=0){
					headerString.append(ProjectConst.SPLIT_KEY_DOC_COLUMN);
				}
				headerString.append(header[i]);
			}
			docDef.setHeader(headerString.toString());
			docId = this.create(docDef);
		}
		
		Set<String> queryIndexSet = new HashSet<String>();
		String queryIndex = docDef.getQueryIndex();
		if(queryIndex != null){
			String [] indexes = queryIndex.split(ProjectConst.SPLIT_KEY_DOC_QUERY_INDEX);
			for (int i = 0; i < indexes.length; i++) {
				String idx = StringUtils.trim(indexes[i]);
				if(StringUtils.isBlank(idx))
					continue;
				queryIndexSet.add(idx);
			}
		}
		
		
		for (int i = hdRowIndex+1; i < data.length; i++) {
			
			StringBuffer text = new StringBuffer();
			MoDocValue docVal = new MoDocValue();
			boolean startFlag = false;
			for (short n = 0; n < data[i].length; n++) {
				if(queryIndexSet.contains(String.valueOf(n))){
					if(docVal.getQueryCol1() == null){
						docVal.setQueryCol1(data[i][n]);
					}else if(docVal.getQueryCol2() == null){
						docVal.setQueryCol2(data[i][n]);
					}else if(docVal.getQueryCol3() == null){
						docVal.setQueryCol3(data[i][n]);
					}
					
				}else if(docDef.getUserIdIndex() == n){
					docVal.setUserId(data[i][n]);
				}else{
					if(startFlag){
						text.append(ProjectConst.SPLIT_KEY_DOC_COLUMN);
					}
					text.append(data[i][n]);
					startFlag = true;
				}
				
			}
			
			docVal.setDocId(docId);
			docVal.setRowId(i);
			docVal.setText(text.toString());
			
			MoDocValue sampleDocVal = new MoDocValue();
			sampleDocVal.setDocId(docId);
			sampleDocVal.setQueryCol1(docVal.getQueryCol1());
			sampleDocVal.setQueryCol2(docVal.getQueryCol2());
			sampleDocVal.setQueryCol3(docVal.getQueryCol3());
			sampleDocVal.setUserId(docVal.getUserId());
			
			if(baseService.list(sampleDocVal).size() > 0){
				baseService.updateAllById(docVal);
			}else{
				baseService.create(docVal);
			}
			
		}
		
	}
	
	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	}

}
